﻿using System;
using System.Web;
using Castle.MonoRail.Framework.Configuration;
using Castle.MonoRail.Framework.Routing;
using Castle.MonoRail.WindsorExtension;
using Castle.Windsor;
using StateTheater.Controllers;
using StateTheater.Helpers;
using Componentx = Castle.MicroKernel.Registration.Component;

namespace StateTheater.Web
{
	/// <summary>
	/// Summary description for StateTheaterApplication
	/// </summary>
	public class StateTheaterApplication : HttpApplication, IContainerAccessor
	{
		protected static WindsorContainer container;

		public StateTheaterApplication()
		{

		}

		public void Application_OnStart()
		{
			container = new StateTheater.MonoRailContainer(MonoRailConfiguration.GetConfig().ControllersConfig.Assemblies);

//			Container.AddComponent("logger", typeof(ILoggerFactory), typeof(TraceLoggerFactory));
//			Container.AddComponentWithProperties<StateTheaterDataContext>("dataContext", DictHelper.CreateN("connection", Util.ConnectionString));

			Container.Register(Componentx.For<IWindsorContainer>().Instance(container));
			Container.Register(Componentx.For<Random>().Instance(new Random()).LifeStyle.Singleton);
//			Container.Register(Componentx.For<string>().Named("WebRoot").Instance(this.Context.Request.ApplicationPath).LifeStyle.Singleton);

			//if (this.Context.Session == null)
			//    Container.Register(Componentx.For<SnitzUtil, SnitzUtil>().LifeStyle.Singleton.DependsOn(new {WebRoot= "/"})); 
			//else
				Container.Register(Componentx.For<SnitzUtil, SnitzUtil>().LifeStyle.Singleton.DependsOn(new {WebRoot= this.Context.Request.ApplicationPath }));

//			Container.Register(Componentx.For<SnitzViewHelper, SnitzViewHelper>().LifeStyle.Singleton);


#if (true)
			PatternRoute pt = new PatternRoute("troupebyid", "/Troupe/[id].rails")
				.DefaultForController().Is<TroupeController>()
				.DefaultForAction().Is("Display")
				.Restrict("id").ValidInteger;
			RoutingModuleEx.Engine.Add(pt);


			pt = new PatternRoute("troupebyname", "/Troupe/[name].rails")
				.DefaultForController().Is<TroupeController>()
				.DefaultForAction().Is("Display")
				.Restrict("name").ValidRegex("[a-zA-Z1-9]+");
			RoutingModuleEx.Engine.Add(pt);

			/*

						pt = new PatternRoute("oldsite", "/?[letter][id]")
						.DefaultForController().Is<AspController>()
							.DefaultForAction().Is("Default")
							.Restrict("id").ValidInteger;

						<rule>
							<pattern>json/(\w+)/(\w+).rails(.*)$</pattern>
							<replace><![CDATA[$1/$2Json.rails?$3 ]]></replace>
						</rule>
			* /
						pt = new PatternRoute("jsonrte", "/json/[ctrlr]/[actionpart].rails[rest]")
						.DefaultForController().Is("[ctrlr]")
							.DefaultForAction().Is("[actionpart]" + "json")
							.
			*/

/*			Handled in AspController
			pt = new PatternRoute("oldprodbyid", "/default.asp?page=production&id=[id]")
			.DefaultForController().Is<ShowController>()
				.DefaultForAction().Is("Display")
				.Restrict("id").ValidInteger;

			pt = new PatternRoute("oldprodbyid2", "?P[id]")
			.DefaultForController().Is<ShowController>()
				.DefaultForAction().Is("Display")
				.Restrict("id").ValidInteger;
			RoutingModuleEx.Engine.Add(pt);

			pt = new PatternRoute("oldtroupebyid", "?T[id]")
				.DefaultForController().Is<TroupeController>()
				.DefaultForAction().Is("Display")
				.Restrict("id").ValidInteger;
			RoutingModuleEx.Engine.Add(pt);
*/

#elif (false)
			RoutingModuleEx.Engine.Add(
			   PatternRule.Build("troupebyname", "Troupe/name/<name:string>.rails", typeof(TroupeController), "Display"));

		   RoutingModuleEx.Engine.Add(
			   PatternRule.Build("troupebyid", "Troupe/<id:number>/display.rails", typeof(TroupeController), "Display"));

			RoutingModuleEx.Engine.Add(
			   PatternRule.Build("venuebyname", "Venue/name/<name:string>.rails", typeof(VenueController), "Display"));

			RoutingModuleEx.Engine.Add(
			   PatternRule.Build("venuebyid", "Venue/<id:number>/display.rails", typeof(VenueController), "Display"));
#endif
		}

		public void Application_OnEnd()
		{
			container.Dispose();
		}

		#region IContainerAccessor Members

		public IWindsorContainer Container
		{
			get { return container; }
		}

		#endregion

#if false
		protected void RegisterFacilities(WindsorContainer container)
		{
			container.AddFacility("rails", new MonoRailFacility());
		}
		protected void RegisterComponents(WindsorContainer container)
		{
//            DefaultComponentInstaller installer = new DefaultComponentInstaller();
			//services.RegisterService(ServiceIdentification.ControllerFactory,
			//             typeof(DefaultControllerFactory));
			container.Installer.SetUp(container, container.Kernel.ConfigurationStore);
			container.AddComponent("audition.controller", typeof(AuditionController));
			container.AddComponent("person.controller", typeof(PersonController));
			//           container.AddComponent("show.controller", typeof(ShowController));
			container.AddComponent("site.controller", typeof(SiteController));
			container.AddComponent("statistic.controller", typeof(AuditionController));
			container.AddComponent("troupe.controller", typeof(TroupeController));
			container.AddComponent("venue.controller", typeof(VenueController));

		}
#endif
	}
}